<?php

namespace admin\modules\test\models\searches;

use Yii;
use common\components\wajaxcrud\generators\crud\SpecialFilterTrait;
use common\components\wtools\tools\Model;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery;
use yii\db\Expression;
use common\components\wtools\tools\ArrayHelper;
use common\models\db\Test as TestModel;

/**
 * Test represents the model behind the search form about `common\models\db\Test`.
 */
class Test extends TestModel
{
    use SpecialFilterTrait;

    const SCENARIO_EDITABLE = 'editable';

    public function rules()
    {
        return [
            [['id', 'updated_at', 'deleted_at', 'created_by', 'updated_by', 'status'], 'safe'],
            [['created_at'], 'match', 'pattern' => '/^.+\s\-\s.+$/'],
        ];
    }

    public function scenarios()
    {
        return ArrayHelper::merge(parent::scenarios(), [
            self::SCENARIO_EDITABLE => [''],
        ]);
    }

    /**
     * Creates data provider instance with search query applied
     * @param array $params
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find();
        $this->load($params);
        $this->fieldFilter($query, 'id');
        $this->rangeFilter($query, 'created_at', true);
        $this->fieldFilter($query, 'updated_at');
        $this->fieldFilter($query, 'deleted_at');
        $this->fieldFilter($query, 'created_by');
        $this->fieldFilter($query, 'updated_by');
        $this->fieldFilter($query, 'status');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC, ]],
            ]);
        if (!$this->validate()) {
            return $dataProvider;
        }
        return $dataProvider;
    }
}
